home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 1.iso / ARGONET / PD / PROGRAMMING / PERL.SPK / gdbm / gdbm-1_7_3 / c / gdbmopen < prev    next >
Text File  |  1996-02-12  |  13KB  |  449 lines

  1. /* gdbmopen.c - Open the dbm file and initialize data structures for use. */
  2.  
  3. /*  This file is part of GDBM, the GNU data base manager, by Philip A. Nelson.
  4.     Copyright (C) 1990, 1991, 1993  Free Software Foundation, Inc.
  5.  
  6.     GDBM is free software; you can redistribute it and/or modify
  7.     it under the terms of the GNU General Public License as published by
  8.     the Free Software Foundation; either version 2, or (at your option)
  9.     any later version.
  10.  
  11.     GDBM is distributed in the hope that it will be useful,
  12.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  13.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14.     GNU General Public License for more details.
  15.  
  16.     You should have received a copy of the GNU General Public License
  17.     along with GDBM; see the file COPYING.  If not, write to
  18.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  19.  
  20.     You may contact the author by:
  21.        e-mail:  phil@cs.wwu.edu
  22.       us-mail:  Philip A. Nelson
  23.                 Computer Science Department
  24.                 Western Washington University
  25.                 Bellingham, WA 98226
  26.  
  27. *************************************************************************/
  28.  
  29.  
  30. /* AIX demands this be the very first thing in the file. */
  31. #if !defined(__GNUC__) && defined(_AIX)
  32.  #pragma alloca
  33. #endif
  34.  
  35. /* include system configuration before all else. */
  36. #include "autoconf.h"
  37.  
  38. #include "gdbmdefs.h"
  39. #include "gdbmerrno.h"
  40. #if HAVE_FCNTL_H
  41. #include <fcntl.h>
  42. #else
  43. #include "fcntl.h"
  44. #endif
  45.  
  46. /* Initialize dbm system.  FILE is a pointer to the file name.  If the file
  47.    has a size of zero bytes, a file initialization procedure is performed,
  48.    setting up the initial structure in the file.  BLOCK_SIZE is used during
  49.    initialization to determine the size of various constructs.  If the value
  50.    is less than 512, the file system blocksize is used, otherwise the value
  51.    of BLOCK_SIZE is used.  BLOCK_SIZE is ignored if the file has previously
  52.    initialized.  If FLAGS is set to GDBM_READ the user wants to just
  53.    read the database and any call to dbm_store or dbm_delete will fail. Many
  54.    readers can access the database at the same time.  If FLAGS is set to
  55.    GDBM_WRITE, the user wants both read and write access to the database and
  56.    requires exclusive access.  If FLAGS is GDBM_WRCREAT, the user wants
  57.    both read and write access to the database and if the database does not
  58.    exist, create a new one.  If FLAGS is GDBM_NEWDB, the user want a
  59.    new database created, regardless of whether one existed, and wants read
  60.    and write access to the new database.  Any error detected will cause a
  61.    return value of null and an approprate value will be in gdbm_errno.  If
  62.    no errors occur, a pointer to the "gdbm file descriptor" will be
  63.    returned. */
  64.  
  65.  
  66. gdbm_file_info *
  67. gdbm_open (file, block_size, flags, mode, fatal_func)
  68.      char *file;
  69.      int  block_size;
  70.      int  flags;
  71.      int  mode;
  72.      void (*fatal_func) ();
  73. {
  74.   gdbm_file_info *dbf;        /* The record to return. */
  75.   struct stat file_stat;    /* Space for the stat information. */
  76.   int         len;        /* Length of the file name. */
  77.   int         num_bytes;    /* Used in reading and writing. */
  78.   off_t       file_pos;        /* Used with seeks. */
  79.   int          lock_val;         /* Returned by the flock call. */
  80.   int          file_block_size;    /* Block size to use for a new file. */
  81.   int           index;        /* Used as a loop index. */
  82.   char        need_trunc;    /* Used with GDBM_NEWDB and locking to avoid
  83.                    truncating a file from under a reader. */
  84.  
  85.   /* Initialize the gdbm_errno variable. */
  86.   gdbm_errno = GDBM_NO_ERROR;
  87.  
  88.   /* Allocate new info structure. */
  89.   dbf = (gdbm_file_info *) malloc (sizeof (gdbm_file_info));
  90.   if (dbf == NULL)
  91.     {
  92.       gdbm_errno = GDBM_MALLOC_ERROR;
  93.       return NULL;
  94.     }
  95.  
  96.   /* Initialize some fields for known values.  This is done so gdbm_close
  97.      will work if called before allocating some structures. */
  98.   dbf->dir  = NULL;
  99.   dbf->bucket = NULL;
  100.   dbf->header = NULL;
  101.   dbf->bucket_cache = NULL;
  102.   dbf->cache_size = 0;
  103.  
  104.   /* Save name of file. */
  105.   len = strlen (file);
  106.   dbf->name = (char *) malloc (len + 1);
  107.   if (dbf->name == NULL)
  108.     {
  109.       free (dbf);
  110.       gdbm_errno = GDBM_MALLOC_ERROR;
  111.       return NULL;
  112.     }
  113.   strcpy (dbf->name, file);
  114.  
  115.   /* Initialize the fatal error routine. */
  116.   dbf->fatal_err = fatal_func;
  117.  
  118.   /* Check for fast writers. */
  119.   if (flags & GDBM_FAST)
  120.     {
  121.       dbf->fast_write = TRUE;
  122.       flags -= GDBM_FAST;
  123.     }
  124.   else
  125.     {
  126.       dbf->fast_write = FALSE;
  127.     }
  128.  
  129.   /* Open the file. */
  130.   need_trunc = FALSE;
  131.   if (flags == GDBM_READER)
  132.     {
  133.       dbf->desc = open (dbf->name, O_RDONLY, 0);
  134.     }
  135.   else if (flags == GDBM_WRITER)
  136.     {
  137.       dbf->desc = open (dbf->name, O_RDWR, 0);
  138.     }
  139.   else if (flags == GDBM_NEWDB)
  140.     {
  141.       dbf->desc = open (dbf->name, O_RDWR|O_CREAT, mode);
  142.       flags = GDBM_WRITER;
  143.       need_trunc = TRUE;
  144.     }
  145.   else
  146.     {
  147.       dbf->desc = open (dbf->name, O_RDWR|O_CREAT, mode);
  148.       flags = GDBM_WRITER;
  149.     }
  150.   if (dbf->desc < 0)
  151.     {
  152.       free (dbf->name);
  153.       free (dbf);
  154.       gdbm_errno = GDBM_FILE_OPEN_ERROR;
  155.       return NULL;
  156.     }
  157.  
  158.   /* Get the status of the file. */
  159.   fstat (dbf->desc, &file_stat);
  160.  
  161.   /* Lock the file in the approprate way. */
  162.   if (flags == GDBM_READER)
  163.     {
  164.       if (file_stat.st_size == 0)
  165.     {
  166.       close (dbf->desc);
  167.       free (dbf->name);
  168.       free (dbf);
  169.       gdbm_errno = GDBM_EMPTY_DATABASE;
  170.       return NULL;
  171.     }
  172.       /* Sets lock_val to 0 for success.  See systems.h. */
  173.       READLOCK_FILE(dbf);
  174.     }
  175.   else
  176.     {
  177.       /* Sets lock_val to 0 for success.  See systems.h. */
  178.       WRITELOCK_FILE(dbf);
  179.     }
  180.   if (lock_val != 0)
  181.     {
  182.       close (dbf->desc);
  183.       free (dbf->name);
  184.       free (dbf);
  185.       if (flags == GDBM_READER)
  186.     gdbm_errno = GDBM_CANT_BE_READER;
  187.       else
  188.     gdbm_errno = GDBM_CANT_BE_WRITER;
  189.       return NULL;
  190.     }
  191.  
  192.   /* Record the kind of user. */
  193.   dbf->read_write = flags;
  194.  
  195.   /* If we do have a write lock and it was a GDBM_NEWDB, it is
  196.      now time to truncate the file. */
  197.   if (need_trunc && file_stat.st_size != 0)
  198.     {
  199.       TRUNCATE (dbf);
  200.       fstat (dbf->desc, &file_stat);
  201.     }
  202.  
  203.   /* Decide if this is a new file or an old file. */
  204.   if (file_stat.st_size == 0)
  205.     {
  206.  
  207.       /* This is a new file.  Create an empty database.  */
  208.  
  209.       /* Start with the blocksize. */
  210.       if (block_size < 512)
  211.     file_block_size = STATBLKSIZE;
  212.       else
  213.     file_block_size = block_size;
  214.  
  215.       /* Get space for the file header. */
  216.       dbf->header = (gdbm_file_header *) malloc (file_block_size);
  217.       if (dbf->header == NULL)
  218.     {
  219.       gdbm_close (dbf);
  220.       gdbm_errno = GDBM_MALLOC_ERROR;
  221.       return NULL;
  222.     }
  223.  
  224.       /* Set the magic number and the block_size. */
  225.       dbf->header->header_magic = 0x13579ace;
  226.       dbf->header->block_size = file_block_size;
  227.  
  228.       /* Create the initial hash table directory.  */
  229.       dbf->header->dir_size = 8 * sizeof (off_t);
  230.       dbf->header->dir_bits = 3;
  231.       while (dbf->header->dir_size < dbf->header->block_size)
  232.     {
  233.       dbf->header->dir_size <<= 1;
  234.       dbf->header->dir_bits += 1;
  235.     }
  236.  
  237.       /* Check for correct block_size. */
  238.       if (dbf->header->dir_size != dbf->header->block_size)
  239.     {
  240.       gdbm_close (dbf);
  241.       gdbm_errno = GDBM_BLOCK_SIZE_ERROR;
  242.       return NULL;
  243.     }
  244.  
  245.       /* Allocate the space for the directory. */
  246.       dbf->dir = (off_t *) malloc (dbf->header->dir_size);
  247.       if (dbf->dir == NULL)
  248.     {
  249.       gdbm_close (dbf);
  250.       gdbm_errno = GDBM_MALLOC_ERROR;
  251.       return NULL;
  252.     }
  253.       dbf->header->dir = dbf->header->block_size;
  254.  
  255.       /* Create the first and only hash bucket. */
  256.       dbf->header->bucket_elems =
  257.     (dbf->header->block_size - sizeof (hash_bucket))
  258.     / sizeof (bucket_element) + 1;
  259.       dbf->header->bucket_size  = dbf->header->block_size;
  260. #if !defined(sgi)
  261.       dbf->bucket = (hash_bucket *) (alloca (dbf->header->bucket_size));
  262. #else    /* sgi */
  263.       /* The SGI C compiler doesn't accept the previous form. */
  264.       {
  265.         hash_bucket *ptr;
  266.     ptr = (hash_bucket *) (alloca (dbf->header->bucket_size));
  267.     dbf->bucket = ptr;
  268.       }
  269. #endif    /* sgi */
  270.       if (dbf->bucket == NULL)
  271.     {
  272.       gdbm_close (dbf);
  273.       gdbm_errno = GDBM_MALLOC_ERROR;
  274.       return NULL;
  275.     }
  276.       _gdbm_new_bucket (dbf, dbf->bucket, 0);
  277.       dbf->bucket->av_count = 1;
  278.       dbf->bucket->bucket_avail[0].av_adr = 3*dbf->header->block_size;
  279.       dbf->bucket->bucket_avail[0].av_size = dbf->header->block_size;
  280.  
  281.       /* Set table entries to point to hash buckets. */
  282.       for (index = 0; index < dbf->header->dir_size / sizeof (off_t); index++)
  283.     dbf->dir[index] = 2*dbf->header->block_size;
  284.  
  285.       /* Initialize the active avail block. */
  286.       dbf->header->avail.size
  287.     = ( (dbf->header->block_size - sizeof (gdbm_file_header))
  288.      / sizeof (avail_elem)) + 1;
  289.       dbf->header->avail.count = 0;
  290.       dbf->header->avail.next_block = 0;
  291.       dbf->header->next_block  = 4*dbf->header->block_size;
  292.  
  293.       /* Write initial configuration to the file. */
  294.       /* Block 0 is the file header and active avail block. */
  295.       num_bytes = write (dbf->desc, dbf->header, dbf->header->block_size);
  296.       if (num_bytes != dbf->header->block_size)
  297.     {
  298.       gdbm_close (dbf);
  299.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  300.       return NULL;
  301.     }
  302.  
  303.       /* Block 1 is the initial bucket directory. */
  304.       num_bytes = write (dbf->desc, dbf->dir, dbf->header->dir_size);
  305.       if (num_bytes != dbf->header->dir_size)
  306.     {
  307.       gdbm_close (dbf);
  308.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  309.       return NULL;
  310.     }
  311.  
  312.       /* Block 2 is the only bucket. */
  313.       num_bytes = write (dbf->desc, dbf->bucket, dbf->header->bucket_size);
  314.       if (num_bytes != dbf->header->bucket_size)
  315.     {
  316.       gdbm_close (dbf);
  317.       gdbm_errno = GDBM_FILE_WRITE_ERROR;
  318.       return NULL;
  319.     }
  320.  
  321.       /* Wait for initial configuration to be written to disk. */
  322.       fsync (dbf->desc);
  323.  
  324.     }
  325.   else
  326.     {
  327.       /* This is an old database.  Read in the information from the file
  328.      header and initialize the hash directory. */
  329.  
  330.       gdbm_file_header partial_header;  /* For the first part of it. */
  331.  
  332.       /* Read the partial file header. */
  333.       num_bytes = read (dbf->desc, &partial_header, sizeof (gdbm_file_header));
  334.       if (num_bytes != sizeof (gdbm_file_header))
  335.     {
  336.       gdbm_close (dbf);
  337.       gdbm_errno = GDBM_FILE_READ_ERROR;
  338.       return NULL;
  339.     }
  340.  
  341.       /* Is the magic number good? */
  342.       if (partial_header.header_magic != 0x13579ace)
  343.     {
  344.       gdbm_close (dbf);
  345.       gdbm_errno = GDBM_BAD_MAGIC_NUMBER;
  346.       return NULL;
  347.     }
  348.  
  349.       /* It is a good database, read the entire header. */
  350.       dbf->header = (gdbm_file_header *) malloc (partial_header.block_size);
  351.       if (dbf->header == NULL)
  352.     {
  353.       gdbm_close (dbf);
  354.       gdbm_errno = GDBM_MALLOC_ERROR;
  355.       return NULL;
  356.     }
  357.       bcopy (&partial_header, dbf->header, sizeof (gdbm_file_header));
  358.       num_bytes = read (dbf->desc, &dbf->header->avail.av_table[1],
  359.             dbf->header->block_size-sizeof (gdbm_file_header));
  360.       if (num_bytes != dbf->header->block_size-sizeof (gdbm_file_header))
  361.     {
  362.       gdbm_close (dbf);
  363.       gdbm_errno = GDBM_FILE_READ_ERROR;
  364.       return NULL;
  365.     }
  366.  
  367.       /* Allocate space for the hash table directory.  */
  368.       dbf->dir = (off_t *) malloc (dbf->header->dir_size);
  369.       if (dbf->dir == NULL)
  370.     {
  371.       gdbm_close (dbf);
  372.       gdbm_errno = GDBM_MALLOC_ERROR;
  373.       return NULL;
  374.     }
  375.  
  376.       /* Read the hash table directory. */
  377.       file_pos = lseek (dbf->desc, dbf->header->dir, L_SET);
  378.       if (file_pos != dbf->header->dir)
  379.     {
  380.       gdbm_close (dbf);
  381.       gdbm_errno = GDBM_FILE_SEEK_ERROR;
  382.       return NULL;
  383.     }
  384.  
  385.       num_bytes = read (dbf->desc, dbf->dir, dbf->header->dir_size);
  386.       if (num_bytes != dbf->header->dir_size)
  387.     {
  388.       gdbm_close (dbf);
  389.       gdbm_errno = GDBM_FILE_READ_ERROR;
  390.       return NULL;
  391.     }
  392.  
  393.     }
  394.  
  395.   /* Finish initializing dbf. */
  396.   dbf->last_read = -1;
  397.   dbf->bucket = NULL;
  398.   dbf->bucket_dir = 0;
  399.   dbf->cache_entry = NULL;
  400.   dbf->header_changed = FALSE;
  401.   dbf->directory_changed = FALSE;
  402.   dbf->bucket_changed = FALSE;
  403.   dbf->second_changed = FALSE;
  404.  
  405.   /* Everything is fine, return the pointer to the file
  406.      information structure.  */
  407.   return dbf;
  408.  
  409. }
  410.  
  411. /* initialize the bucket cache. */
  412. int
  413. _gdbm_init_cache(dbf, size)
  414.     gdbm_file_info *dbf;
  415.     int size;
  416. {
  417. register int index;
  418.  
  419.   if (dbf->bucket_cache == NULL)
  420.     {
  421.       dbf->bucket_cache = (cache_elem *) malloc(sizeof(cache_elem) * size);
  422.       if(dbf->bucket_cache == NULL)
  423.         {
  424.           gdbm_errno = GDBM_MALLOC_ERROR;
  425.           return(-1);
  426.         }
  427.       dbf->cache_size = size;
  428.  
  429.       for(index = 0; index < size; index++)
  430.         {
  431.           (dbf->bucket_cache[index]).ca_bucket
  432.             = (hash_bucket *) malloc (dbf->header->bucket_size);
  433.           if ((dbf->bucket_cache[index]).ca_bucket == NULL)
  434.         {
  435.               gdbm_errno = GDBM_MALLOC_ERROR;
  436.           return(-1);
  437.             }
  438.           (dbf->bucket_cache[index]).ca_adr = 0;
  439.           (dbf->bucket_cache[index]).ca_changed = FALSE;
  440.           (dbf->bucket_cache[index]).ca_data.hash_val = -1;
  441.           (dbf->bucket_cache[index]).ca_data.elem_loc = -1;
  442.           (dbf->bucket_cache[index]).ca_data.dptr = NULL;
  443.         }
  444.       dbf->bucket = dbf->bucket_cache[0].ca_bucket;
  445.       dbf->cache_entry = &dbf->bucket_cache[0];
  446.     }
  447.   return(0);
  448. }
  449.